package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.exception.ServiceException;
import com.sdleyou.treepro.common.model.convert.MessageDO2MessageVOConverter;
import com.sdleyou.treepro.common.model.dao.MessageDO;
import com.sdleyou.treepro.common.model.dao.Page;
import com.sdleyou.treepro.common.model.enums.ErrorCodeEnum;
import com.sdleyou.treepro.common.model.utils.RandomUtil;
import com.sdleyou.treepro.common.model.vo.MessageVO;
import com.sdleyou.treepro.common.service.MongoPageManager;
import com.sdleyou.treepro.common.service.UserMessageManager;
import com.sdleyou.treepro.common.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserMessageManagerImpl implements UserMessageManager {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private MongoPageManager mongoPageManager;

    @Override
    public void create(MessageVO messageVO) {
        MessageDO messageDO = new MessageDO();
        BeanUtils.copyProperties(messageVO, messageDO);
        messageDO.setMessageId(RandomUtil.randomId());
        messageDO.setGmtCreate(DateUtil.getDateline());
        messageDO.setDateFormat(DateUtil.toString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        messageDO.setIsNew(true);
        messageDO.setStatus("ACTIVE");
        mongoTemplate.insert(messageDO);
        return;
    }

    @Override
    public Page<MessageVO> list(Integer pageSize, Integer pageNum, String userId) {
        Query query = new Query(Criteria.where("status").is("ACTIVE").and("user_id").is(userId).and("gmt_create").gte(DateUtil.getPreviousUnix(2)/1000));
        Sort sort = Sort.by(Sort.Direction.DESC, "gmt_create");
        query.with(sort);
        Page<MessageDO> page = mongoPageManager.pageQuery(query, MessageDO.class, pageSize, pageNum);
        List<MessageDO> messageDOList = page.getData();
        List<MessageVO> messageVOList = messageDOList.stream().map(e -> MessageDO2MessageVOConverter.convert(e)).collect(Collectors.toList());
        return new Page<>(page.getPageNo(), page.getDataTotal(), page.getPageSize(), messageVOList);
    }

    @Override
    public void read(String messageId) {
        Query query = new Query(Criteria.where("message_id").is(messageId).and("is_new").is(true));
        MessageDO messageDO = mongoTemplate.findOne(query, MessageDO.class);
        if (messageDO == null) {
            throw new ServiceException(ErrorCodeEnum.E019.code(), ErrorCodeEnum.E019.getDescription());
        }
        Update update = new Update();
        update.set("is_new", false);
        mongoTemplate.updateFirst(query, update, MessageDO.class);
        return;
    }

    @Override
    public void delete(String messageId) {
        Query query = new Query(Criteria.where("message_id").is(messageId).and("status").is("ACTIVE"));
        MessageDO messageDO = mongoTemplate.findOne(query, MessageDO.class);
        if (messageDO == null) {
            throw new ServiceException(ErrorCodeEnum.E019.code(), ErrorCodeEnum.E019.getDescription());
        }
        Update update = new Update();
        update.set("status", "DELETE");
        mongoTemplate.updateFirst(query, update, MessageDO.class);
        return;
    }
}
